define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'integral/outtasks/index',
                    add_url: 'integral/outtasks/release/ids/'+task_ids,
                    edit_url: 'integral/outtasks/edit',
                    del_url: 'integral/outtasks/delete',
                    check_url: 'tasks/task/check/type/1/task_id/'+task_ids,
                    table: 'finance_extract_task',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                  searchFormVisible: true,
                searchFormTemplate: 'customformtpl',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('ID')},
                        {field: 'status_text', title: __('Task.Status'),
                        table: table,
                        events: Controller.api.events.StatusDesc,
                        buttons: [],
                        formatter: Controller.api.formatter.StatusDesc
                        },
                        {field: 'school_text', title: __('School.School_id')},
                        {field: 'createtime', title: __("Createtime"), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __("Updatetime"), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,buttons: [],formatter:Controller.api.formatter.operate}


                    ]
                ],
                queryParams: function (params) {
                  //这里可以追加搜索条件
                  var filter = JSON.parse(params.filter);
                   //这里可以动态赋值，比如从URL中获取admin_id的值，filter.admin_id=Fast.api.query('admin_id');
                  filter.task_id = task_ids;
                  filter.delete_status = 1;
                  params.filter = JSON.stringify(filter);
                  return params;
                }
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        release: function () {

            $("#sch_wen").blur(function () {
                plus_sch('sch');
            });
            $("#sch_li").blur(function () {
                plus_sch('sch');
            });
            $("#class_wen").blur(function () {
                plus_sch('class');
            });
            $("#class_li").blur(function () {
                plus_sch('class');
            });


            //学校文理科人数
            function plus_sch(id){
                if (id==null)return;
                var li = $("#"+id+"_li").val();
                if (parseFloat(li).toString() == "NaN") {li=Number(li)}

                var wen = $("#"+id+"_wen").val();
                if (parseFloat(wen).toString() == "NaN") {wen=Number(wen)}
                if (li==0 && wen==0) return;
                $("#"+id).val(parseInt(parseInt(li)+parseInt(wen)));
            }
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter:{
                browser: function (value, row, index) {
                    //这里我们直接使用row的数据
                    return '<a class="btn btn-xs btn-browser" href="http://www.baidu.com">1111</a>';
                }, 
                StatusDesc: function (value, row, index) {
                      
                    return '<a class="btn btn-xs btn-ip"> '+value+ '</a>';
                },
                view:function (value,row,index) {
                    var title="填写任务";
                    var table = this.table;
                    // 默认按钮组
                    var buttons = [];
                    buttons.push({
                        name: 'detail',
                        title: title,
                        text: title,
                        classname: 'btn btn-xs btn-warning btn-dialog',
                        url: 'integral/outtasks/release',
                        extend:'data-area=\'["80%","80%"]\'',
                    });
                    return Table.api.buttonlink(this, buttons, value, row, index, 'buttons');


                },
                operate: function (value, row, index) {
                    var table = this.table;
                    // 操作配置
                    var options = table ? table.bootstrapTable('getOptions') : {};
                    // 默认按钮组
                    var buttons = $.extend([], this.buttons || []);
                    var check = 0;
                    var review = 0;
                    switch (true) {
                        case row.status == 1 || row.status == 2:
                            check = 1;
                          break;
                        default:
                           break;
                    }
                    if(check){
                          buttons.push({
                            name: 'check',
                            icon: 'fa fa-paper-plane',
                            title: __('Check'),
                            extend: 'data-toggle="tooltip"',
                            classname: 'btn btn-xs btn-success btn-checkone',
                            url: options.extend.check_url
                          });
                        buttons.push({
                              name: 'edit',
                              icon: 'fa fa-pencil',
                              title: __('Edit'),
                              extend: 'data-toggle="tooltip" data-area=\'["60%","80%"]\'',
                              classname: 'btn btn-xs btn-success btn-editone',
                          url: options.extend.edit_url
                        });
                        buttons.push({
                          name: 'del',
                          icon: 'fa fa-trash',
                          title: __('Del'),
                          extend: 'data-toggle="tooltip"',
                          classname: 'btn btn-xs btn-danger btn-delone',
                          url: options.extend.del_url
                        });
                    }
                    return Table.api.buttonlink(this, buttons, value, row, index, 'operate');
                },

            },
             events: {//绑定事件的方法

                StatusDesc: {
                    //格式为：方法名+空格+DOM元素
                    'click .btn-ip': function (e, value, row, index) {
                        e.stopPropagation();
                           var content_data ='<table  class="table table-striped table-bordered table-hover table-nowrap" data-operate-edit="1" data-operate-del="1" width="100%"><thead><tr> <th style="text-align: center;">'+__('Task.Date')+'</th><th style="text-align: center;">'+__('Task.Status')+'</th><th style="text-align: center;">'+__('Task.Check')+'</th><th style="text-align: center;">'+__('Task.Publish')+'</th><th style="text-align: center;">'+__('Status_desc')+'</th></tr></thead><tbody id="ultable" >';
                             content_data += row.status_desc+'</tbody></table>';
                             layer.open({
                               type: 1,
                               skin: 'layui-layer-demo', //样式类名
                               title:'审核记录',
                               //closeBtn: 0, //不显示关闭按钮
                               anim: 2,
                               shadeClose: true, //开启遮罩关闭
                               area: ['50%', '50%'],
                               
                               content: content_data
                            });
                    }
                }
            },

        }
    };
    return Controller;
});